﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace ChuyenDongVatLy
{
    public partial class FrDaoDongDieuHoa : Form
    {
        public FrDaoDongDieuHoa()
        {
            this.Location = new Point(ConstPhysic.locationFormX, ConstPhysic.locationFormY);
            InitializeComponent();
            if (tbKhoiLuong.Text == null || tbDoCung.Text == null || tbBienDo.Text == null || tbVTBD_X.Text == null || tbVTBD_Y.Text == null || tbPhaBD.Text == null)
                btsave.Enabled = false;
            else
                btsave.Enabled = true;
        }
        public delegate void GetString(string khoiluong,string docung,string biendo,string vitriX,string vitriY, string phabandau);
        public GetString GetMyValue;
        public delegate void GetString2(bool createfrom);
        public GetString2 GetMyValue2;
        private void btOK_Click(object sender, EventArgs e)
        {

            if (tbBienDo.Text == "" || tbDoCung.Text == "" || tbKhoiLuong.Text == "" || tbPhaBD.Text == "" || tbVTBD_X.Text == "" || tbVTBD_Y.Text == "")
            {
                MessageBox.Show("Ban phai dien day du thong so!!!");
            }
            else
            {
                float m = float.Parse(tbKhoiLuong.Text);
                float x = float.Parse(tbVTBD_X.Text);
                float A = float.Parse(tbBienDo.Text);
                if (m < 1 || m > 10 || A < 50 || A > 250 || x < A || x > 2 * A)
                {
                    if (m < 1 || m > 10)
                        MessageBox.Show("khối lượng vượt quá giới hạn 1->10 kg");

                    if (A < 50 || A > 250)
                    {
                        MessageBox.Show("Biên độ dao vượt quá giới hạn 50->250 cm");
                    }
                    if (x < A || x > 2 * A)
                    {
                        MessageBox.Show("Vị trí ban đầu không phù hợp với biên độ");
                    }
                   
                }
                else
                {
                    GetMyValue(tbKhoiLuong.Text, tbDoCung.Text, tbBienDo.Text, tbVTBD_X.Text, tbVTBD_Y.Text, tbPhaBD.Text);
                }
                
            }
        }

        private void btCanCle_Click(object sender, EventArgs e)
        {
            if (GetMyValue2 != null)
            {
                GetMyValue2(true);
            }
            this.Close();
        }

        private void tbKhoiLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbDoCung_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }

        }

        private void tbBienDo_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbVTBD_X_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbVTBD_Y_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void tbPhaBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);

            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && tbKhoiLuong.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void FrDaoDongDieuHoa_Load(object sender, EventArgs e)
        {

        }

        private void btsave_Click(object sender, EventArgs e)
        {
            //btsave.Enabled = false;
            if (tbKhoiLuong.Text == "" || tbDoCung.Text == "" || tbBienDo.Text == "" || tbVTBD_X.Text == "" || tbVTBD_Y.Text == "" || tbPhaBD.Text == "")
                MessageBox.Show("Cần điền đầy đủ thông sô !!!");
            else
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Title = "Save as";
                saveFile.Filter = "*.txt|";
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    FileStream f = new FileStream(saveFile.FileName.ToString() + ".txt", FileMode.Create);
                    StreamWriter srw = new StreamWriter(f);
                    srw.Write(tbKhoiLuong.Text);
                    srw.WriteLine();
                    srw.Write(tbDoCung.Text);
                    srw.WriteLine();
                    srw.Write(tbBienDo.Text);
                    srw.WriteLine();
                    srw.Write(tbVTBD_X.Text);
                    srw.WriteLine();
                    srw.Write(tbVTBD_Y.Text);
                    srw.WriteLine();
                    srw.Write(tbPhaBD.Text);
                    srw.WriteLine();
                    srw.Close();
                    f.Close();
                }
            }            
        }

        private void btload_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFile.RestoreDirectory = true;
            DialogResult result = openFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                string filename = openFile.FileName;
                string[] lines = File.ReadAllLines(filename);
                tbKhoiLuong.Text = lines[0].ToString();
                tbDoCung.Text = lines[1].ToString();
                tbBienDo.Text = lines[2].ToString();
                tbVTBD_X.Text = lines[3].ToString();
                tbVTBD_Y.Text = lines[4].ToString();
                tbPhaBD.Text = lines[5].ToString();
            }            
        }
                     
    }
}
